package com.bst.base.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.bst.base.vo.MdMedDimFldVO;
import com.bst.common.vo.HeaderAndBodyVO;
import com.bst.etl.service.ITableMetaQueryService;
import com.bst.etl.vo.TableFieldVO;
import com.bst.md.domain.MdMedDim;
import com.bst.md.service.IMdMedDimService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bst.common.annotation.Log;
import org.springframework.stereotype.Controller;
import com.bst.common.enums.BusinessType;
import com.bst.base.domain.MdMedDimFld;
import com.bst.base.service.IMdMedDimFldService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 维度字段Controller
 * 
 * @author laoliu
 * @date 2024-11-16
 */
@Controller
@RequestMapping("/base/md/dim/fld")
public class MdMedDimFldController extends BaseController
{
    private String prefix = "base/base/md/dim";

    @Autowired
    private IMdMedDimFldService mdMedDimFldService;
    @Autowired
    private IMdMedDimService mdMedDimService;
    @Autowired
    ITableMetaQueryService tableMetaQueryService;

    @GetMapping()
    public String fld(String tableName, ModelMap mmap)
    {
        MdMedDim dim = mdMedDimService.selectMdMedDimByTableName(tableName);
        MdMedDimFld fld = new MdMedDimFld();
        fld.setCdTb(tableName);
        List<MdMedDimFld> dimFlds = mdMedDimFldService.selectMdMedDimFldList(fld);
        if(dimFlds.size()==0) {
            List<TableFieldVO> fieldVOS = tableMetaQueryService.findAllFldsByTableName(tableName,"dw");
            MdMedDimFld dimFld;
            for (TableFieldVO vo : fieldVOS) {
                dimFld = new MdMedDimFld();
                dimFld.setCdTb(tableName);
                dimFld.setCd(vo.getName());
                dimFlds.add(dimFld);
            }
        }
        mmap.put("dim",dim);
        mmap.put("dimFlds",dimFlds);
        return prefix + "/dim_fld";
    }

    @GetMapping("/dim/dim_data")
    public String dim_data(String tableName, ModelMap mmap)
    {
        MdMedDim dim = mdMedDimService.selectMdMedDimByTableName(tableName);
        MdMedDimFld fld = new MdMedDimFld();
        fld.setCdTb(tableName);
        List<MdMedDimFld> dimFlds = mdMedDimFldService.selectMdMedDimFldList(fld);
        String sql="select top 100 ";
        Map<String,String> headerMap = new LinkedHashMap<>();
        int i=0;
        for (MdMedDimFld dimFld : dimFlds) {
            sql+=dimFld.getCd()+",";
            headerMap.put(dimFld.getCd(),dimFld.getNa());
            if(i>4) {
                break;
            }
        }
        sql += " from "+dim.getCdTb();
        HeaderAndBodyVO vo = new HeaderAndBodyVO();
        vo.setThead(headerMap);
        //vo.setTbody();
        mmap.put("tableData",vo);
        return prefix + "/dim_data";
    }

    /**
     * 查询维度字段列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdMedDimFld mdMedDimFld)
    {
        startPage();
        List<MdMedDimFld> list = mdMedDimFldService.selectMdMedDimFldList(mdMedDimFld);
        return getDataTable(list);
    }

    /**
     * 导出维度字段列表
     */
    @Log(title = "维度字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdMedDimFld mdMedDimFld)
    {
        List<MdMedDimFld> list = mdMedDimFldService.selectMdMedDimFldList(mdMedDimFld);
        ExcelUtil<MdMedDimFld> util = new ExcelUtil<MdMedDimFld>(MdMedDimFld.class);
        return util.exportExcel(list, "维度字段数据");
    }

    /**
     * 新增维度字段
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存维度字段
     */
    @Log(title = "维度字段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdMedDimFld mdMedDimFld)
    {
        return toAjax(mdMedDimFldService.insertMdMedDimFld(mdMedDimFld));
    }

    /**
     * 修改维度字段
     */
    @GetMapping("/edit/{idDimFld}")
    public String edit(@PathVariable("idDimFld") Long idDimFld, ModelMap mmap)
    {
        MdMedDimFld mdMedDimFld = mdMedDimFldService.selectMdMedDimFldByIdDimFld(idDimFld);
        mmap.put("mdMedDimFld", mdMedDimFld);
        return prefix + "/edit";
    }

    /**
     * 修改保存维度字段
     */
    @Log(title = "维度字段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdMedDimFldVO fldVO)
    {
        return toAjax(mdMedDimFldService.insertBatchMdMedDimFld(fldVO));
    }

    /**
     * 删除维度字段
     */
    @Log(title = "维度字段", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdMedDimFldService.deleteMdMedDimFldByIdDimFlds(ids));
    }
}
